
package at.ac.tuwien.ir;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.zip.GZIPOutputStream;

import at.ac.tuwien.ir.controller.CommandLineReader;
import at.ac.tuwien.ir.controller.DocumentController;
import at.ac.tuwien.ir.controller.FileController;
import at.ac.tuwien.ir.controller.IDocumentController;
import at.ac.tuwien.ir.controller.ISimilarityController;
import at.ac.tuwien.ir.controller.IProbabilityController;
import at.ac.tuwien.ir.controller.ProbabilityController;
import at.ac.tuwien.ir.controller.SimilarityController;
import at.ac.tuwien.ir.controller.Tokenizer;
import at.ac.tuwien.ir.model.ProgramFunction;

public class InformationRetrieval {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Program started");
		CommandLineReader reader = new CommandLineReader(args);
		reader.parseArgs();
		
		if(reader.getProgramFunction() == ProgramFunction.GenerateIndex) {
		
			IDocumentController documentController = new DocumentController(new FileController(), new Tokenizer(), reader.getIndexSize());
			documentController.generateDocumentDescription(reader.getDocumentsDirectory());
			
			String indexFilename = reader.getIndexFilename() + "_" +  reader.getIndexSize().toString() + ".arff";
			documentController.generateIndexFile(indexFilename);
			
			File file = new File(indexFilename);
			double fileSize = file.length() / 1024 / 1024;
			if(fileSize > reader.get_gzipThreshold()) {
				System.out.println("gzipping index file");
				try {
					String line;
					BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(indexFilename + ".gzip"))));
					BufferedReader bufferedReader = new BufferedReader(new FileReader(indexFilename));
					
					while( (line = bufferedReader.readLine()) != null ) {
						writer.write(line);
						writer.newLine();
					}
					
					writer.close();
					bufferedReader.close();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
			
		} else if(reader.getProgramFunction() == ProgramFunction.ComputeSimilarityMeasure) {
			 ISimilarityController similarityController = new SimilarityController();
			 similarityController.generateSimilarityFile(reader.getIndexFilename(), reader.getOutputDirectory());
			 
		} else if(reader.getProgramFunction() == ProgramFunction.ComputeProbabilisticMeasure) {
			 IProbabilityController probabilityController = new ProbabilityController(new FileController(), new Tokenizer(), reader.getDocumentsDirectory());
			 probabilityController.generateProbabilityFile(reader.getOutputDirectory());
		}
		System.out.println("Program finished");
	}

}

